package utils;

import android.util.Log;

import com.wenxin.administrator.zhufengfm.Constants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import entity.DiscoverCategory;
import entity.DiscoverRecommend.DiscoverRecommendAlbums;
import entity.DiscoverRecommend.DiscoverRecommendColumns;
import entity.DiscoverRecommend.DiscoverRecommendItem;

/**
 * Created by Administrator on 15-10-20.
 */
public class EntityParseUtil {
    private EntityParseUtil() {
    }

    public static List<DiscoverCategory> parseDiscoverCategories(JSONObject json) {
        List<DiscoverCategory> ret = null;
        if (json != null) {
            try {
                int code = json.getInt("ret");
                if (code == Constants.TASK_RESULT_OK) {
                    ret = new LinkedList<>();
                    JSONArray array = json.getJSONArray("list");
                    int len = array.length();
                    if (len > 0) {
                        for (int i = 0; i < len; i++) {
                            JSONObject obj = array.getJSONObject(i);
                            DiscoverCategory category = new DiscoverCategory();
                            //利用实体类进行json解析
                            category.parseJSON(obj);
                            ret.add(category);
                        }
                    }
                }
                else{

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }

    /**
     * 解析“发现”模块中 推荐 栏目中的数据结构
     * @param jsonObject
     * @return
     */
   public static List<DiscoverRecommendItem> parseDiscoverRecommends(JSONObject jsonObject) {
       List<DiscoverRecommendItem> ret=null;
       Log.d("debug11","json = "+jsonObject);
       if(jsonObject==null){
           Log.d("debug11","json为空");
       }else
           Log.d("debug11","json="+jsonObject.toString());

       if(jsonObject!=null) {
           try {
               int code=jsonObject.getInt("ret");
               if(code==Constants.TASK_RESULT_OK)
               {
                   ret=new ArrayList<>();
                   JSONObject objectAlbums=jsonObject.getJSONObject("editorRecommendAlbums");
                   //小编推荐 内容解析
                   DiscoverRecommendAlbums editorRecommend=new DiscoverRecommendAlbums();
                   editorRecommend.parseJSON(objectAlbums);
                   ret.add(editorRecommend);
                   //todo 解析精品听单
                   JSONObject objectColumn=jsonObject.getJSONObject("specialColumn");
                   DiscoverRecommendColumns listener=new DiscoverRecommendColumns();
                   listener.parseJSON(objectColumn);
                   ret.add(listener);

                   //todo  解析发现新奇

                   //解析热门推荐
                   JSONObject objectRecommend=jsonObject.getJSONObject("hotRecommends");
                   JSONArray arrayRecommend=objectRecommend.getJSONArray("list");
                   int len=arrayRecommend.length();
                   for (int i = 0; i <len ; i++) {
                       //获取热门推荐内部List中的每一个对象信息
                       JSONObject hostList=arrayRecommend.getJSONObject(i);
                       DiscoverRecommendAlbums e=new DiscoverRecommendAlbums();
                       e.parseJSON(hostList);
                       ret.add(e);
                   }
               }
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
        return ret;
    }
}
